<template>
  <div>
    <el-row :gutter="20" type="flex" justify="center">
      <!-- 获奖列表 -->
      <el-col :span="23"
        ><div class="grid-content bg-purple">
          <el-card class="box-card">
            <div class="mask">
              <!-- 搜索与添加区域 -->
              <el-row :gutter="20">
                <el-col :span="8">
                  <div class="searchBox">
                    <el-select
                      v-model="searchType"
                      placeholder="请选择搜索类型"
                      @change="handleSearchSelectInputChange"
                    >
                      <el-option
                        v-for="item in searchTypeOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      >
                      </el-option>
                    </el-select>
                    <el-input
                      :placeholder="searchTypePlaceholder"
                      v-model="searchInputKey"
                      clearable
                      @input="essaySearchInputChange"
                    >
                    </el-input>
                  </div>
                </el-col>
              </el-row>

              <el-container style="height: 86%" width="100%">
                <el-main>
                  <!-- 获奖列表区域 -->
                  <el-table :data="awardList" border stripe>
                    <el-table-column type="index" label="ID"></el-table-column>
                    <el-table-column label="获奖名称" prop="name">
                      <template slot-scope="scope">
                        <div
                          class="essayName"
                          @click="handleAwardDetailShow(scope.row)"
                        >
                          {{ scope.row.name }}
                        </div>
                      </template>
                    </el-table-column>
                    <el-table-column
                      label="获奖作者"
                      prop="awardee"
                      width="100"
                    >
                    </el-table-column>
                    <el-table-column
                      label="所属单位"
                      prop="awardee_unit"
                      width="200px"
                    ></el-table-column>
                    <el-table-column
                      label="获奖级别"
                      prop="award_category"
                      width="102px"
                    ></el-table-column>
                    <el-table-column
                      label="获奖等级"
                      prop="award_level"
                    ></el-table-column>
                    <el-table-column
                      label="学科分类"
                      prop="subject_classification"
                    ></el-table-column>
                    <el-table-column
                      label="下达单位"
                      prop="release_unit"
                    ></el-table-column>
                    <el-table-column label="状态" prop="status" width="66px">
                      <template slot-scope="scope">
                        <span v-if="scope.row.status == 1">已通过</span>
                        <span v-else-if="scope.row.status == 0">未通过</span>
                      </template>
                    </el-table-column>
                  </el-table>
                </el-main>
              </el-container>

              <!-- 分页区域 -->
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[1, 2, 5, 10]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
              >
              </el-pagination>
            </div>
          </el-card></div
      ></el-col>
    </el-row>
    <myAwardDetail></myAwardDetail>
  </div>
</template>

<script>
import myAwardDetail from "../../components/award/myAwardDetail.vue";

import { mapState } from "vuex";

export default {
  name: "myAward",
  components: { myAwardDetail },
  data() {
    return {
      // 获取获奖列表的参数对象
      queryInfo: {
        query: "",
        // 当前的页数
        pagenum: 1,
        // 当前每页显示多少条数据
        pagesize: 5,
      },
      searchInputKey: "",
      // 搜索类型
      searchType: "name",
      // 获奖信息
      awardList: [],
      total: 0,

      // 搜索类型
      searchTypeOptions: [
        {
          value: "name",
          label: "获奖名称",
        },
        {
          value: "author",
          label: "获奖第一作者",
        },
        {
          value: "subject_classification",
          label: "学科分类",
        },
        {
          value: "award_category",
          label: "获奖级别",
        },
        {
          value: "award_level",
          label: "获奖等级",
        },
      ],
      // 搜索placeholder
      searchTypePlaceholder: "搜索获奖名称",
    };
  },
  methods: {
    // 获取获奖列表
    async getAwardList() {
      const { data: res } = await this.$axios.get("/award", {
        params: {
          queryInfo: this.queryInfo,
          searchType: this.searchType,
          username: this.moreUserInfo.username,
        },
      });
      if (res.status !== 200) {
        return this.$message.error("获取获奖信息失败");
      }
      // this.$message.success("获取获奖信息成功");
      this.awardList = res.result;
      this.total = res.total;
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage);
      this.queryInfo.pagenum = newPage;
      this.getAwardList();
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize;
      this.getAwardList();
    },
    // 搜索功能
    async essaySearchInputChange() {
      // console.log(this.queryInfo);
      this.queryInfo.query = this.searchInputKey;
      this.getAwardList();
    },
    // 查看获奖详情
    handleAwardDetailShow(data) {
      this.$bus.$emit("handleAwardDetailShow", data);
    },
    // 搜索类型
    handleSearchSelectInputChange(val) {
      try {
        this.searchTypeOptions.forEach((item) => {
          if (item.value == val) {
            this.searchTypePlaceholder = `搜索${item.label}`;
            throw new Error("");
          }
        });
      } catch (err) {}
    },
  },
  computed: {
    ...mapState(["subject_classificationList", "collegeList", "moreUserInfo"]),
  },
  mounted() {
    this.getAwardList();
    this.$bus.$on("handleGetAwardList", this.getAwardList);
  },
};
</script>

<style lang="less" scoped>
.searchBox {
  display: flex;
  width: 100%;
}

.status {
  span {
    margin-left: 20px;
  }
}
.essayName {
  cursor: pointer;
  background-color: rgba(185, 184, 184, 0.1);
  border-radius: 20px;
  text-align: center;
  font-weight: 700;
}

// 获奖列表
.project-title {
  width: 100%;
  text-align: center;
  margin-bottom: 20px;
}

.mask {
  overflow: hidden;
  height: 0vh;
  animation: maskAni 1s forwards;
}

// 内容
.mask.content {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  justify-content: space-around;
}

@keyframes maskAni {
  100% {
    height: 82vh;
  }
}
</style>